package com.zhiyou100.review.day014.homework;

import cn.hutool.core.util.RandomUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * @packageName: Homework
 * @className: com.zhiyou100.review.day014.homework
 * @Description: TODO day14 作业
 * @author: yang
 * @date: 6/22/20
 */
public class Homework {
    /**
     * @Description: TODO calendar类随机2010-1-1 0:0:0到2020-6-22 23:59:59之间的时间
     * @name: myRandomDate
     * @param: []
     * @return: java.util.Date
     * @date: 6/22/20 18:54
     * @author: yang
     */
    public Calendar myRandomDate() {
        Calendar start = Calendar.getInstance();
        Calendar end = (Calendar) start.clone();
        start.set(2010, 1, 1, 0, 0, 0);
        end.set(2020, 6, 22, 23, 59, 59);
        long time = RandomUtil.randomLong(start.getTimeInMillis(), end.getTimeInMillis());
        // 随机 2010-1-1 0:0:0 到 2020-6-22 23:59:59 之间的 毫秒值

        Calendar finalTime = (Calendar) end.clone();
        finalTime.setTimeInMillis(time);
        return finalTime;
    }

    /**
     * @Description: TODO 打印calendar如：xxxx-xx-xx 星期x xx:xx:xx
     * @name: myPrintCalendar
     * @param: [calendar]
     * @return: java.lang.String
     * @date: 6/22/20 19:09
     * @author: yang
     */
    public String myPrintCalendar(Calendar calendar) {
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        char finalWeek = " 日一二三四五六".charAt(week);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int seconds = calendar.get(Calendar.SECOND);

        return year + "-" + month + "-" + day + " 星期" + finalWeek + " " + hour + ":" + minute + ":" + seconds;
    }


    /**
     * @Description: TODO 由参数calendar对象 获取指定格式的字符串
     * @name: getOneStringOfCalendar
     * @param: [calendar]
     * @return: java.lang.String
     * @date: 6/22/20 19:15
     * @author: yang
     */
    public String getOneStringOfCalendar(Calendar calendar) {
        Date time = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年-MM月-dd天 星期u HH:mm:ss");
        return simpleDateFormat.format(time);
    }


    /**
     * @Description: TODO   由指定格式的参数字符串 获取一个calendar对象
     * @name: getOneCalendarOfString
     * @param: [str]
     * @return: java.util.Calendar
     * @date: 6/22/20 19:20
     * @author: yang
     */
    public Calendar getOneCalendarOfString(String str) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年-MM月-dd天 星期u HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        try {
            Date parse = simpleDateFormat.parse(str);
            calendar.setTime(parse);
        } catch (ParseException e) {
            throw new RuntimeException("字符串格式错误");
        }
        return calendar;
    }

    public static void main(String[] args) {
        Homework homework = new Homework();
        Calendar instance = Calendar.getInstance();
    }
}

class AgeLT0Exception extends RuntimeException {
    public AgeLT0Exception() {
        super("年龄不能小于0");
    }

}

class AgeGT150Exception extends RuntimeException {
    public AgeGT150Exception() {
        super("年龄不能大于 150");
    }
}

class Studnet {
    private String name;
    private int age;

    public Studnet() {
    }

    @Override
    public String toString() {
        return "Studnet{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    /**
     * @Description: TODO 如果用户在给学生年龄赋值时， 年龄小于0抛出一个AgeLT0Exception，大于150 抛出一个AgeGT150Exception
     * @name: setAge
     * @param: [age]
     * @return: void
     * @date: 6/22/20 19:28
     * @author: yang
     */
    public void setAge(int age) {
        int maxAge = 150;
        if (age < 0) {
            throw new AgeLT0Exception();

        } else if (age > maxAge) {
            throw new AgeGT150Exception();
        }

    }

    public Studnet(String name, int age) {
        this.name = name;
        setAge(age);
    }
}

